<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模板编码" prop="templateCode">
        <el-input
          v-model="queryParams.templateCode"
          placeholder="请输入模板code"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="商家模板" prop="serviceTemplateCode">
        <el-input
          v-model="queryParams.serviceTemplateCode"
          placeholder="请输入服务商模板"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sms:smsTemplate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sms:smsTemplate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sms:smsTemplate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sms:smsTemplate:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="smsTemplateList"
      :default-sort="tableSort"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="模板code" align="center" prop="templateCode" />
      <el-table-column label="系统服务名" align="center" prop="sysServiceName" />
      <el-table-column label="服务商" align="center" prop="serviceProvider" />
      <el-table-column label="服务商模板" align="center" prop="serviceTemplateCode" />
      <el-table-column label="模板名称" align="center" prop="templateName" />
      <el-table-column label="模板内容" align="center" prop="templateBody" />
      <el-table-column label="模板参数" align="center" prop="templateParams" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" sortable="custom">
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sms:smsTemplate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sms:smsTemplate:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page="queryParams.pageNum"
      :limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改短信模板对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px" append-to-body  >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模板code" prop="templateCode">
          <el-input v-model="form.templateCode" placeholder="请输入模板code" />
        </el-form-item>
        <el-form-item label="系统服务名" prop="sysServiceName">
          <el-input v-model="form.sysServiceName" placeholder="请输入系统服务名" />
        </el-form-item>
        <el-form-item label="服务商" prop="serviceProvider">
          <el-input v-model="form.serviceProvider" placeholder="请输入服务商" />
        </el-form-item>
        <el-form-item label="服务商模板" prop="serviceTemplateCode">
          <el-input v-model="form.serviceTemplateCode" placeholder="请输入服务商模板" />
        </el-form-item>
        <el-form-item label="模板名称" prop="templateName">
          <el-input v-model="form.templateName" placeholder="请输入模板名称" />
        </el-form-item>
        <el-form-item label="模板内容" prop="templateBody">
          <el-input v-model="form.templateBody" placeholder="请输入模板内容" />
        </el-form-item>
        <el-form-item label="模板参数" prop="templateParams">
          <el-input v-model="form.templateParams" placeholder="请输入模板参数" />
        </el-form-item>
        <el-form-item label="状态(1是,0否)">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in sys_normal_disable"
              :key="dict.value"
:label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
          <div class="dialog-footer">
            <el-button @click="handleDialogCancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
          </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SmsTemplate">
import { ref, toRefs } from "vue";
import { listSmsTemplate, getSmsTemplate, delSmsTemplate, addSmsTemplate, updateSmsTemplate, exportXlsUrl } from "@/api/sms/smsTemplate";

const { proxy } = getCurrentInstance();

  const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
  // 遮罩层
  const loading = ref(true);
  // 选中数组
  const ids = ref([]);
  // 非单个禁用
  const single = ref(true);
  // 非多个禁用
  const multiple = ref(true);
  // 显示搜索条件
  const showSearch = ref(true);
  // 总条数
  const total = ref(0);
  // 短信模板表格数据
  const smsTemplateList  = ref([]);
  // 弹出层标题
  const dialogTitle  = ref("");
  // 是否显示弹出层
  const dialogVisible  = ref(false);
  // 查询参数
     const reactiveData  = reactive(
     {
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          templateCode: null,
          sysServiceName: null,
          serviceProvider: null,
          serviceTemplateCode: null,
          templateName: null,
          templateBody: null,
          templateParams: null,
          status: null,
            params: {
              sort: 'createTime',
              order: 'descending',
            }
        },
        form: {},
        rules: {
        templateCode: [
          { required: true, message: "模板code不能为空", trigger: "blur" }
        ],
        serviceTemplateCode: [
          { required: true, message: "服务商模板不能为空", trigger: "blur" }
        ],
        templateName: [
          { required: true, message: "模板名称不能为空", trigger: "blur" }
        ],
        templateBody: [
          { required: true, message: "模板内容不能为空", trigger: "blur" }
        ],
        }
     });

      const {queryParams,form,rules} = toRefs(reactiveData);

      const tableSort = computed(()=>{
        if(queryParams.value.params.sort){
          return {
            prop: queryParams.value.params.sort,
            order: queryParams.value.params.order
          }
        }
        return null;
      })

    /** 查询短信模板列表 */
   function getList() {
     loading.value = true;
      listSmsTemplate(queryParams.value).then(response => {
        smsTemplateList.value = response.data.records;
        total.value = response.data.total;
        loading.value = false;
      });
    }
    // 取消按钮
    function handleDialogCancel() {
      dialogVisible.value = false;
      reset();
    }
    // 表单重置
    function reset() {
      form.value = {
        id: null,
        templateCode: null,
        sysServiceName: null,
        serviceProvider: null,
        serviceTemplateCode: null,
        templateName: null,
        templateBody: null,
        templateParams: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      proxy.resetForm("formRef");
    }
    /** 搜索按钮操作 */
   function handleQuery() {
      queryParams.value.pageNum = 1;
      getList();
    }
    /** 重置按钮操作 */
    function resetQuery() {
      proxy.resetForm("queryFormRef");
      handleQuery();
    }

    // 多选框选中数据
    function handleSelectionChange(selection) {
      ids.value = selection.map(item => item.id)
      single.value = selection.length!==1
      multiple.value = !selection.length
    }

    // 排序
    function handleSortChange({ column, prop, order }){
      queryParams.value.params.sort = prop?prop:null;
      queryParams.value.params.order = order?order:null;
      getList();
    }

    /** 新增按钮操作 */
    function handleAdd() {
      reset();
      dialogVisible.value = true;
      dialogTitle.value = "添加短信模板";
    }

    /** 修改按钮操作 */
    function handleUpdate(row) {
      reset();
      const id = row.id || ids.value
      getSmsTemplate(id).then(response => {
        form.value = response.data;
        dialogVisible.value = true;
        dialogTitle.value = "修改短信模板";
      });
    }
    /** 提交按钮 */
    function submitForm() {
      proxy.$refs["formRef"].validate(valid => {
        if (valid) {
          if (form.value.id != null) {
            updateSmsTemplate(form.value).then(response => {
              proxy.$modal.msgSuccess("修改成功");
              dialogVisible.value = false;
              getList();
            });
          } else {
            addSmsTemplate(form.value).then(response => {
              proxy.$modal.msgSuccess("新增成功");
              dialogVisible.value = false;
              getList();
            });
          }
        }
      });
    }
    /** 删除按钮操作 */
   function handleDelete(row) {
      const ids = row.id || ids.value;
      proxy.$modal.confirm('是否确认删除短信模板编号为"' + ids + '"的数据项？').then(function() {
        return delSmsTemplate(ids);
      }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
   /** 导出按钮操作 */
   function handleExport() {
      proxy.download(exportXlsUrl, {
        ...queryParams.value
      }, `smsTemplate_${new Date().getTime()}.xlsx`)
    }
   /** 初始化数据 **/
   getList();
</script>
